Page({
  data: {
    frontFile: '/images/idcardfront.png', // 身份证正面
    backFile: '/images/idcardback.png', // 身份证反面
    showSubmit: false,
    yp: '',
    idcardType: 0,
  },
  onReady: function(e) {
    //获得dialog组件
    this.dialog = this.selectComponent("#dialog");
  },
  showDialog: function() {
    this.dialog.showDialog();
  },
  //取消事件
  _cancelEvent: function() {
    console.log('你点击了取消');
    this.dialog.hideDialog();
    this.wxChooseImage(this.data.idcardType);
  },
  chooseImage: function(e) {
    var that = this;
    var idtype = e.target.dataset.type;
    that.setData({
      idcardType: idtype
    });
    // 首次样片展示
    var showSample = that.showSample(idtype);
    if (!showSample) {
      that.wxChooseImage(idtype);
    }
  },
  wxChooseImage: function(idtype) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        if (0 == idtype) {
          // 正面
          that.setData({
            frontFile: res.tempFilePaths[0]
          });
        } else if (1 == idtype) {
          // 反面
          that.setData({
            backFile: res.tempFilePaths[0]
          });
        }

        // 启用提交按钮
        if (that.data.frontFile.indexOf('idcardfront.png') == -1 && that.data.backFile.indexOf('idcardback.png') == -1) {
          that.setData({
            showSubmit: true
          });
        }
      }
    })
  },
  previewImage: function(e) {
    if (!this.data.files ) return;
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  // 提交
  submit: function() {
    this.data.files;
    this.setData({
      showSubmit: false
    });
    this.imageUpData();

  },
  /**
  * 上传提交
  */
  imageUpData: function () {
    var me = this;
    wx.request({
      url: "http://10.5.107.66/pub/Mysoft.Slxt.ProjectPrep.Interfaces.IRoomMngPublicService/SetCustomerData",
      method: 'POST',
      data: {
        FileGuids: ["17e35d4c-0b82-e811-bb65-8c1645295c8b", "18e35d4c-0b82-e811-bb65-8c1645295c8b"],
        CustomerDataGUID: "E12AE4B7-A8AC-48B0-8D9D-BBA80400239E"
      },
      header: {
        'AppId': 'qianyuezhushou',
        'AppKey': '1e3619c2eeccbd1e',
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        me.setData({
          logMsg: res
        });
        wx.redirectTo({
          url: '../common/success'
        });

      },
      fail: function (da) {
        me.setData({
          errMsg: da.errMsg
        });
 
      }
    })

  },

  // 上传图片
  uploadFile: function(file, i) { //递归调用
    var that = this;
    wx.uploadFile({
      url: 'http://localhost:8080/web/uploadImage', //仅为示例，非真实的接口地址
      filePath: file[i],
      name: 'file',
      success: function(res) {
        var obj = new Object();
        obj.ind = i + 1;
        var data = res.data;
        var resultData = JSON.parse(res.data);
        that.setData({
          imageUrl: resultData.url
        });
      }
    })
  },

  // 弹出样品提示
  showSample: function(idtype) {
    var that = this;
    if (that.data.frontFile.indexOf('http') > -1) {
      return false;
    }
    if (0 == idtype) {
      // 正面
      that.setData({
        yp: "/images/yp_front.png"
      });
    } else if (1 == idtype) {
      // 反面
      that.setData({
        yp: "/images/yp_back.png"
      });
    }
    that.showDialog();
    return true;
  }

});